#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <dirent.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>

#include "data_type.h"
#include "cube.h"
#include "cube_define.h"
#include "cube_record.h"

#include "user_define.h"
#include "record_define.h"
#include "action_define.h"

// add para lib_include
#include "transfer.h"


int proc_dispatch(void * sub_proc,void * message);

int transfer_init(void * sub_proc,void * para)
{
	int ret;
	// add youself's plugin init func here
	return 0;
}

int transfer_start(void * sub_proc,void * para)
{
	int ret;
	void * recv_msg;
	int i;
	int type;
	int subtype;


	while(1)
	{
		usleep(time_val.tv_usec);
		ret=ex_module_recvmsg(sub_proc,&recv_msg);
		if(ret<0)
			continue;
		if(recv_msg==NULL)
			continue;
		type=message_get_type(recv_msg);
		subtype=message_get_subtype(recv_msg);
		if(!memdb_find_recordtype(type,subtype))
		{
			printf("message format (%d %d) is not registered!\n",
				message_get_type(recv_msg),message_get_subtype(recv_msg));
			continue;
		}
		if((type==TYPE(RECORD_DEFINE))&&(subtype==SUBTYPE(RECORD_DEFINE,SEND)))
		{
			ret=proc_transfer_addtarget(sub_proc,recv_msg);
		}
		if((type==TYPE(ACTION_DEFINE))&&(subtype==SUBTYPE(ACTION_DEFINE,HANDSHAKE)))
		{
			ret=proc_handshake_addtarget(sub_proc,recv_msg);
		}
	}
	return 0;
};

int proc_transfer_addtarget(void * sub_proc,void * recv_msg)
{
	int ret;

	RECORD(MESSAGE,UUID_RECORD) * target;
	RECORD(USER_DEFINE,SERVER_STATE) * server_state;
	RECORD(RECORD_DEFINE,SEND) * record_send;
	
	DB_RECORD * db_record;



        ret=message_get_record(recv_msg,&record_send,0);
        if(ret<0)
                return ret;
	
        if(record_send==NULL)
                return -EINVAL;

	target=Talloc0(sizeof(*target));
	db_record=memdb_find_first(TYPE_PAIR(USER_DEFINE,SERVER_STATE),"user_name",record_send->receiver);
        if(db_record==NULL)
        {
                printf("user %s has not logined yet!\n",record_send->receiver);
                return -EINVAL;
        }

	server_state=db_record->record;

	comp_proc_uuid(server_state->node_uuid,server_state->proc_name,target->uuid);

        ret=message_add_expand_data(recv_msg,TYPE_PAIR(MESSAGE,UUID_RECORD),target);
        if(ret<0)
                return ret;
        ex_module_sendmsg(sub_proc,recv_msg);
	return 0;
}
int proc_handshake_addtarget(void * sub_proc,void * recv_msg)
{
	int ret;

	RECORD(MESSAGE,UUID_RECORD) * target;
	RECORD(USER_DEFINE,SERVER_STATE) * server_state;
	RECORD(ACTION_DEFINE,HANDSHAKE) * handshake_info;
	
	DB_RECORD * db_record;



        ret=message_get_record(recv_msg,&handshake_info,0);
        if(ret<0)
                return ret;
	
        if(handshake_info==NULL)
                return -EINVAL;

	target=Talloc0(sizeof(*target));

    if(handshake_info->step %2 == 0)
    {
	    db_record=memdb_find_first(TYPE_PAIR(USER_DEFINE,SERVER_STATE),"user_name",handshake_info->client);
        if(db_record==NULL)
        {
             printf("user %s has not logined yet!\n",handshake_info->server);
            return -EINVAL;
        }
    }
    else
    {
	    db_record=memdb_find_first(TYPE_PAIR(USER_DEFINE,SERVER_STATE),"user_name",handshake_info->server);
        if(db_record==NULL)
        {
             printf("user %s has not logined yet!\n",handshake_info->client);
            return -EINVAL;
        }
    }

	server_state=db_record->record;

	comp_proc_uuid(server_state->node_uuid,server_state->proc_name,target->uuid);

    ret=message_add_expand_data(recv_msg,TYPE_PAIR(MESSAGE,UUID_RECORD),target);
    if(ret<0)
        return ret;
    ex_module_sendmsg(sub_proc,recv_msg);
	return 0;
}
